package com.enation.app.javashop;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Javashop网关规则定义
 *
 * @author kingapex
 * @version 1.0
 * @since 7.0.0
 * 2018/8/14
 */
@Configuration
public class JavashopRouteLocator {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder) {

        return routeBuilder.routes()
                .route("goods-service",
                        route -> route
                                .path("/seller/goods/**").or()
                                .path("/admin/goods/**").or()
                                .path("/admin/goodssearch/**").or()
                                .path("/goods/**").or()
                                .path("/load-customwords/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://goods-service")
                )
                .route("system-service",
                        route -> route
                                .path("/pages/**").or()
                                .path("/express/**").or()
                                .path("/seller/express/**").or()
                                .path("/seller/waybill/**").or()
                                .path("/trade/order-complains/topics/**").or()
                                .path("/captchas/**").or()
                                .path("/uploaders/**").or()
                                .path("/regions/**").or()
                                .path("/site-show/**").or()
                                .path("/ueditor/**").or()
                                .path("/validator").or()
                                .path("/net-image/base64").or()
                                .path("/debugger/validator/test").or()
                                .path("/admin/pages/**").or()
                                .path("/admin/settings/**").or()
                                .path("/admin/systems/**").or()
                                .path("/admin/sensitive-words/**").or()
                                .path("/admin/task/**").or()
                                .path("/webjars/system/base/state/**").or()
                                .path("/seller/pages/**").or()
                                .path("/seller/uploaders/**").or()
                                .path("/seller/system-logs/**").or()
                                .path("/admin/uploaders/**").or()
                                .path("/admin/passport/**").or()
                                .path("/admin/system-logs/**")

                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://system-service")
                )
                .route("payment-service",
                        route -> route
                                .path("/admin/payment/**").or()
                                .path("/payment/**").or()
                                .path("/admin/trade/orders/pay-log/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://payment-service")
                ).route("trade-pay-service",
                        route -> route
                                .path("/order/pay/order/**").or()
                                .path("/order/pay/trade/**").or()
                                .path("/order/pay/needpay/**").or()
                                .path("/order/pay/app/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://trade-service")
                ).route("rewritepath_route",
                        route -> route
                                .path("/order/pay/**").filters(f -> f.rewritePath("/order/pay/(?<segment>.*)","/payment/$\\{segment}"))
                                .uri("lb://payment-service")
                )
                .route("trade-service",
                        route -> route
                                .path("/after-sales/**").or()
                                .path("/buyer/after-sales/**").or()
                                .path("/order/**").or()
                                .path("/balance/pay/**").or()
                                .path("/trade/**").or()
                                .path("/pintuan/**").or()
                                .path("/admin/promotion/pintuan/**").or()
                                .path("/seller/promotion/pintuan/**").or()
                                .path("/seller/after-sales/**").or()
                                .path("/seller/order/**").or()
                                .path("/seller/trade/**").or()
                                .path("/recharge/**").or()
                                .path("/admin/after-sales/**").or()
                                .path("/admin/order/**").or()
                                .path("/jquery.min.js").or()
                                .path("/admin/trade/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://trade-service")
                ).route("promotion-service",
                        route -> route
                                .path("/promotions/**").or()
                                .path("/seller/promotion/**").or()
                                .path("/admin/promotion/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://promotion-service")
                )
                .route("member-service",
                        route -> route
                                .path("/members/**").or()
                                .path("/shops/**").or()
                                .path("/passport/connect/wechat/auth/back").or()
                                .path("/passport/**").or()
                                .path("/connect/**").or()
                                .path("/account-binder/**").or()
                                .path("/login-binder/**").or()
                                .path("/mobile-binder/**").or()
                                .path("/wechat/**").or()
                                .path("/qq/**").or()
								.path("/alipay/**").or()
								.path("/apple/**").or()
								.path("/weibo/**").or()
                                .path("/seller/members/**").or()
                                .path("/seller/shops/**").or()
                                .path("/seller/login/**").or()
                                .path("/seller/register/**").or()
                                .path("/seller/check/**").or()
                                .path("/admin/members/**").or()
                                .path("/admin/shops/**").or()
                                .path("/webjars/system/buyer/state/**").or()
                                .path("/webjars/system/seller/state/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://member-service")
                )
                .route("statistics-service",
                        route -> route
                                .path("/seller/statistics/**").or()
                                .path("/admin/statistics/**").or()
                                .path("/admin/index/**").or()
                                .path("/view")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://statistics-service")
                )
                .route("distribution-service",
                        route -> route
                                .path("/distribution/**").or()
                                .path("/admin/distribution/**").or()
                                .path("/seller/distribution/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://distribution-service")
                )
                .route("live-video-service",
                        route -> route
                                .path("/live-video/**").or()
                                .path("/admin/live-video/**").or()
                                .path("/seller/live-video/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://live-video-service")
                ).route("im-service",
                        route -> route
                                .path("/buyer/im/**").or()
                                .path("/seller/im/**")
                                .filters(gatewayFilterSpec -> {
                                    gatewayFilterSpec.preserveHostHeader();
                                    return gatewayFilterSpec;
                                })
                                .uri("lb://im-service")
                )
                .build();
    }
}
